package org.sgr.money.manager.frame;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.sgr.money.manager.biz.SubjectBiz;
import org.sgr.money.manager.dto.SubjectDto;

public class SubjectMgr extends Composite {

	private Button btnAddSubject = null;
	private Button btnModify = null;
	private Button btnDel = null;
	private Tree tree = null;

	public SubjectMgr(Composite parent, int style) {
		super(parent, style);
		initialize();
	}

	private void initialize() {
		btnAddSubject = new Button(this, SWT.NONE);
		btnAddSubject.setText("新增科目");
		FormData formDataBtnAdd = new FormData();
		formDataBtnAdd.top = new FormAttachment(0, 20);
		formDataBtnAdd.left = new FormAttachment(0, 10);
		formDataBtnAdd.width = 100;
		btnAddSubject.setLayoutData(formDataBtnAdd);
		btnAddSubject.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				System.out.println("mouseUp()");
				TreeItem[] tis = tree.getSelection();
				SubjectDto subjectDto = null;
				if (tis != null && tis.length > 0) {
					Object obj = tis[0].getData();
					subjectDto = (SubjectDto) obj;

					Shell child = new Shell(getShell(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
					FormLayout formlayout = new FormLayout();
					child.setLayout(formlayout);
					new SubjectMgrModify(child, getStyle(), subjectDto,null);
					int cx = 600;
					int cy = 290;
					child.setSize(new Point(cx, cy));
					child.setText("新增科目");
					int lx = getShell().getLocation().x + getShell().getSize().x / 2 - cx / 2;
					int ly = getShell().getLocation().y + getShell().getSize().y / 2 - cy / 2;
					child.setLocation(lx, ly);
					child.open();
				} else {
					MessageBox box = new MessageBox(getShell(), SWT.ICON_WARNING | SWT.OK);
					box.setText("注意");
					// 设置对话框显示的消息
					box.setMessage("请选择父级科目再点击新增!");
					// 打开对话框，将返回值赋给choice
					box.open();
				}
			}
		});
		btnModify = new Button(this, SWT.NONE);
		btnModify.setText("修改科目");
		FormData formDataBtnModify = new FormData();
		formDataBtnModify.top = new FormAttachment(0, 60);
		formDataBtnModify.left = new FormAttachment(0, 10);
		formDataBtnModify.width = 100;
		FormData formDataBtnDel = new FormData();
		formDataBtnDel.top = new FormAttachment(0, 100);
		formDataBtnDel.left = new FormAttachment(0, 10);
		formDataBtnDel.width = 100;
		btnModify.setLayoutData(formDataBtnModify);
		btnDel = new Button(this, SWT.NONE);
		btnDel.setText("删除科目");
		btnDel.setLayoutData(formDataBtnDel);
		tree = new Tree(this, SWT.BORDER);
		tree.setHeaderVisible(false);
		tree.setLinesVisible(false);
		FormData formDataTree = new FormData();
		formDataTree.top = new FormAttachment(0, 10);
		formDataTree.left = new FormAttachment(0, 120);
		formDataTree.bottom = new FormAttachment(98, 0);
		formDataTree.right = new FormAttachment(98, 0);
		tree.setLayoutData(formDataTree);

		createSubjectTree();

		setSize(new Point(500, 280));
		setLayout(new FormLayout());
	}

	private void createSubjectTree() {

		SubjectBiz subjectBiz = new SubjectBiz();
		List<SubjectDto> subjectDtoList = subjectBiz.getAllSubjectTree();
		TreeItem lastTree = null;
		TreeItem cTree = null;
		SubjectDto lastDto = null;
		for (SubjectDto dto : subjectDtoList) {
			if (lastTree != null && lastDto != null
					&& dto.getSummarySubCode().intValue() == lastDto.getSubCode().intValue()) {
				cTree = new TreeItem(lastTree, getStyle());
			} else {
				cTree = new TreeItem(tree, getStyle());
			}
			cTree.setText(dto.getSubName());
			cTree.setData(dto);
			lastTree = cTree;
			lastDto = dto;
		}
	}
}
